define(function(require, exports, module) {
    var router = require("router");
    //var app = require("app");
    var controller = {};
    controller.vue = function() {
        controller.vm = new Vue({
            router: router,
            data: function() {
                return {
                    item: {},
                    publish_text: '确认提交',
                    is_publish: 0,
                    type:'cash',
                    mounted:0,
                    time_limit:0,
                    store:{},
                    showLimitTimeTex:'设置可使用时间段',
                    uploadUrl:app.params.apiDomain+"apiv2/card/uploadimg"
                };
            },
            el: '#pageAddCard',
            created: function() {
                this.fetchData();
            },
            methods: {
                fetchData: function() {
                    var _this = this;
                  this.appid =this.$route.params.appid;
                    this.getStore();

                },
                getStore:function(){
                    var _this = this;
                    var userInfo = app.getUserInfo();
                    app.get("api/user/info",{fields:'store'},function(res){
                        // alert(123);
                        if(res.code!=200){
                            app.alert(res.msg);
                            return;
                        }
                        _this.store = res.data.store;  
                    });
                },
                set_time_limit:function(time_limit){
                    this.time_limit = time_limit;
                },
                set_type:function(type){
                  this.type= type ;
                },

                 
                saveTime:function(){
                     // var data = $("#pageAddCard").find("#popup_time_limit").serializeArray();
                     var showText = "";
                     var data ={};
                     var time_limit={};
                      data.type = $("[name='date_info_type']:checked").val();
                     if(data.type=="DATE_TYPE_FIX_TIME_RANGE"){//固定日期
                          data.begin_timestamp = $("#begin_timestamp").val();
                          data.end_timestamp = $("#end_timestamp").val();
                         if(data.end_timestamp==="" || data.begin_timestamp===""){
                            app.alert("请选择固定日期");
                            return;
                         }
                         showText+="有效期:" +data.begin_timestamp+"~"+data.end_timestamp ;

                     }else{
                            data.fixed_begin_term=$("[name='fixed_begin_term']").val();
                            data.fixed_term=$("[name='fixed_term']").val();
                            showText+="有效期:领取后"+(data.fixed_begin_term>0?data.fixed_begin_term:'当')+"天内生效,";
                            showText+="有效天数"+data.fixed_term+"天";
                    }
                   
                    var type = $("[name='time_limit']:checked").val();
                     
                    if(type==1){
                        time_limit.type=[];
                        showText+="。可用日期:";
                         var length = $("[name='time_limit_type']:checked").length;
                         
                         if(length===0){
                             app.alert("请选择可用日期");
                             return ;
                         }
                         $.each($("[name='time_limit_type']:checked"),function(i,time_limit_type){
                             time_limit.type.push($(time_limit_type).val());
                             showText+=$(time_limit_type).parent().text();
                         });
                         time_limit.begin_hour = $("#time_limit_begin_hour").val();
                         time_limit.end_hour = $("#time_limit_end_hour").val();
                         if(time_limit.begin_hour==="" || time_limit.end_hour===""){
                            app.alert("请选择可用时间");
                            return;
                         }
                         showText+=time_limit.begin_hour+"~"+time_limit.end_hour;
                          
                    }else{
                        showText+="。可用时间段:全天";
                    }
                    this.showLimitTimeTex = showText;
                    this.time_limit = time_limit;
                    this.date_info = data;
                    app.hide_popup();
                    
                    
                    
                },
                publish: function() {
                    var _this = this;
                    if (_this.is_publish === 1) {
                        return;
                    }
                     _this.is_publish = 1;
                    // this.showLimitTimeTex = showText;
                    // this.time_limit = time_limit;
                    // this.date_info = data;
                    var data = $("#pageAddCard").find("#addCardForm").serializeArray();
                    data.push({
                        name:"time_limit",
                        value: JSON.stringify(this.time_limit)
                    });
                    data.push({
                        name:"date_info",
                        value: JSON.stringify(this.date_info)
                    });
                    data.push({
                        name:"showLimitTimeText",
                        value: this.showLimitTimeTex
                    });
                     data.push({
                        name:"appid",
                        value: this.appid
                    });

                    
                    // console.log(data);

                    app.post("apiv2/card/create_"+_this.type,data,function(res){
                       _this.is_publish =0;
                        if(res.code!=200){
                            app.alert(res.msg);
                            return;
                        }
                        app.setPageData("index",{reload:1});
                        app.alert("创建成功",function(){
                                _this.$router.go(-2);

                        });

                    });
                    

                },
                show_popup_time_limit:function(){
                   app.show_popup($("#popup_time_limit"));
                 }
            },
            
             mounted: function() {
                // console.log("123");
                if (this.mounted == 1) {
                    return;
                }
               
                app.lCalendar("end_timestamp");
                app.lCalendar("begin_timestamp");
                app.lCalendar("time_limit_begin_hour");
                app.lCalendar("time_limit_end_hour");
                this.mounted = 1;

            },

        }).$mount('#app');
    };
    module.exports = controller;
});
